//
//  SelectRateViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/22.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "SelectRateViewController.h"
#import "LeoMsg.h"

@interface SelectRateViewController ()

@end

@implementation SelectRateViewController
@synthesize motherLabel;
@synthesize motherWindow;
@synthesize popUpView;
@synthesize tableView;
@synthesize rateTableViewCell;
@synthesize cancelButton;
@synthesize confirmButton;
@synthesize tableArray;
@synthesize selectResult;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackGroudView];
    [self worktable];
    [self initData];
    [self initButton];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [self initData];
}

- (void)initButton{
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchDown];
    [confirmButton addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchDown];
    
    [cancelButton.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    [cancelButton.layer setBorderWidth:1.0]; //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 200/255.0,197/255.0,191/255.0, 1 });
    [cancelButton.layer setBorderColor:colorref];//边框颜色
    
    [confirmButton.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    [confirmButton.layer setBorderWidth:1.0]; //边框宽度
    [confirmButton.layer setBorderColor:colorref];//边框颜色

    
}

- (void)cancelAction{
    self.motherWindow.hidden = YES;
}

- (void)confirmAction{
    
    BOOL isAllUnSelected=true;
    
    
    for (int i = 0; i < 7; i++) {
        if ([[ChooseStatusArray objectAtIndex:i] isEqualToNumber:[NSNumber numberWithInt:1]]) {
            if (![selectResult isEqualToString:@""]) {
                [selectResult appendString:@","];
            }
            [selectResult appendString:[self.tableArray objectAtIndex:i]];
            isAllUnSelected=false;
        }
        else
            isAllSelected = NO;
    }
    if (isAllUnSelected)
    {
        [LeoMsg showMsg:@"请至少选择一项!"];
        return;
    }
    
    if (isAllSelected) {
        self.motherLabel.text = @"每天";
    }
    else
        self.motherLabel.text = selectResult;
    self.motherWindow.hidden = YES;
}

- (void)setBackGroudView{
    [self.view setFrame:[UIScreen mainScreen].bounds];
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
    self.popUpView.layer.cornerRadius = 8;
}

-(void)worktable{
    self.automaticallyAdjustsScrollViewInsets=NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled=NO;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate=self;
    tableView.dataSource=self;
    //tableView.separatorInset = UIEdgeInsetsZero;
    UINib *cellRate = [UINib nibWithNibName:@"RateTableViewCell" bundle:nil];
    [tableView registerNib:cellRate forCellReuseIdentifier:@"ratetableviewcell"];
    self.rateTableViewCell  = [tableView dequeueReusableCellWithIdentifier:@"ratetableviewcell"];
    
}

-(void)initData{
    isAllSelected = YES;
    selectResult = [[NSMutableString alloc] initWithString:@""];
    self.tableArray = [NSArray arrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日", nil];
    if (motherLabel.text==NULL){
        ChooseStatusArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 7; i++) {
            [ChooseStatusArray addObject:[NSNumber numberWithInt:0]];
        }
        return;
    }
    ChooseStatusArray = [[NSMutableArray alloc]init];
    if ([motherLabel.text isEqualToString:@"每天"]){
        for (int i = 0; i < 7; i++) {
            [ChooseStatusArray addObject:[NSNumber numberWithInt:1]];
        }
        return;
    }
    else{
        NSString *rateStr=motherLabel.text;
        int i=0; int x=1;
        while (i<rateStr.length){
            NSString *tempStr=[rateStr substringWithRange:NSMakeRange(i,2)];
            if ([self transferRateStrToInt:tempStr]==x){
                [ChooseStatusArray addObject:[NSNumber numberWithInt:1]];
                x=x+1;  i=i+3;
                 }else{
                     for (int j=x;j<[self transferRateStrToInt:tempStr];j++)
                     {
                         [ChooseStatusArray addObject:[NSNumber numberWithInt:0]];
                         x=x+1;  
                     }
                 }
            
        }
        for (int i=ChooseStatusArray.count;i<7;i++)
        {
            [ChooseStatusArray addObject:[NSNumber numberWithInt:0]];
        }
    
    }
//    ChooseStatusArray = [[NSMutableArray alloc]init];
//    for (int i = 0; i < 7; i++) {
//        [ChooseStatusArray addObject:[NSNumber numberWithInt:0]];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ratetableviewcell"];
    cell.navController=self.navigationController;
    cell = [cell getandSetData:[self.tableArray objectAtIndex:indexPath.row]];
    if ([[ChooseStatusArray objectAtIndex:indexPath.row] isEqualToNumber:[NSNumber numberWithInt:0]]) {
        [cell setChooseStatus:NO];
    }
    else if([[ChooseStatusArray objectAtIndex:indexPath.row] isEqualToNumber:[NSNumber numberWithInt:1]]) {
        [cell setChooseStatus:YES];
    }

    
    cell=[self addCellLine:cell forIndex:0];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RateTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([[ChooseStatusArray objectAtIndex:indexPath.row] isEqualToNumber:[NSNumber numberWithInt:0]]) {
        [ChooseStatusArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:1]];
        [cell setChooseStatus:YES];
    }
    else if([[ChooseStatusArray objectAtIndex:indexPath.row] isEqualToNumber:[NSNumber numberWithInt:1]]) {
        [ChooseStatusArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:0]];
        [cell setChooseStatus:NO];
    }
    
}

-(UITableViewCell *)addCellLine:(UITableViewCell *)cell forIndex:(int)index
{
    //NSArray *dic=self.mydelegate.alertDetailsDataArray;
    UILabel *line;
    line=[[UILabel alloc]initWithFrame:CGRectMake(0, 45, [UIScreen mainScreen].bounds.size.width-0, 1)];
        line.backgroundColor=[UIColor colorWithRed:226/255.0 green:226/255.0 blue:228/255.0 alpha:1.0];
    [cell addSubview:line];
    return cell;
}

-(int)transferRateStrToInt:(NSString*)rateStr{
    int k=0;
    if ([rateStr isEqualToString:@"周一"]){k=1;}
    if ([rateStr isEqualToString:@"周二"]){k=2;}
    if ([rateStr isEqualToString:@"周三"]){k=3;}
    if ([rateStr isEqualToString:@"周四"]){k=4;}
    if ([rateStr isEqualToString:@"周五"]){k=5;}
    if ([rateStr isEqualToString:@"周六"]){k=6;}
    if ([rateStr isEqualToString:@"周日"]){k=7;}
    return k;
}


@end
